var time = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    message: [],
    page: 1,
    txt:"",
    statusBarHeight: app.globalData.statusBarHeight //状态栏高度
  },
  onLoad: function() {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/index',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        console.log(res)
        for (var i = 0; i < res.data.message.length; i++) {
          // 字符串
          if (res.data.data[i].type == 2) {
            if (res.data.data[i].content.length > 24) {
              res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
            }
          }
          // 时间
          let d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
          res.data.data[i].createtime = time.timeHandle(d);
        }
        that.setData({
          message: res.data.data
        })
        console.log(res)
      }
    })
  },
  //获取input框的值
  findInput: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/index',
      data: {
        openid: openid,
        name: e.detail.value
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data.data) {
          // 字符串
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            let d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
          }
        }
        that.setData({
          message: res.data.data ? res.data.data : []
        })
      }
    })
  },
  //跳转到分类发现页面
  goFaXian: function() {
    wx.navigateTo({
      url: 'faxian-fenlei/faxian-fenlei',
    })
  },
  //跳转到具体详情页面
  goSpecific: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/chanpinjuti/chanpin?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //跳转文章详情页面
  goWenZhang: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/wenzhangjuti/wenzhang?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //上拉加载更多
  onReachBottom: function() {
    var openid = wx.getStorageSync("openid");
    var that = this;
    this.setData({
      page: this.data.page + 1
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/index',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
            //添加到已有数组message
            that.data.message.push(res.data.data[i])
          }
          that.setData({
            message: that.data.message,
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500,
            mask:true
          })
        }
      }
    });
  },
  //下拉刷新
  onPullDownRefresh: function() {
    var openid = wx.getStorageSync("openid");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/find/index',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        for (var i = 0; i < res.data.data.length; i++) {
          //字符串
          if (res.data.data[i].type == 2) {
            if (res.data.data[i].content.length > 24) {
              res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
            }
          }
          //时间
          let d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
          res.data.data[i].createtime = time.timeHandle(d);
        }
        that.setData({
          message: res.data.data,
          page:1
        })
        wx.stopPullDownRefresh()
      }
    })
  }
})